package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Manage;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class ClassStuChangeG
 */
@WebServlet("/ClassStuChangeG")
public class ClassStuChangeG extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClassStuChangeG() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		response.setContentType("text/json;charset=utf-8");
		String cla_id = request.getParameter("cla_id");
		String g_id = request.getParameter("g_id");
		Manage man = new Manage();
		try {
			man = man.ClassStuChangeG(cla_id , g_id);
			System.out.println("ok");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONArray jsons=new JSONArray();
		JSONObject json=new JSONObject();
		for(int i = 0; i<man.getCla_num();i++)
		{
			String c_name[] = new String[5];
			double grade[] = new double[5];
			Arrays.fill(grade, -1);
			double a_point = 0,b_point = 0 , point = 0;
				int n = 0;
				json.put("stu_name", man.getCourse().get(i).getStu_name());
				c_name[n] = man.getCourse().get(i).getC_name();
				grade[n] = man.getCourse().get(i).getGrade();
				n++;
				a_point = a_point + man.getCourse().get(i).getC_point()*man.getCourse().get(i).getGpa();
				b_point = b_point + man.getCourse().get(i).getC_point();
				System.out.println(a_point);
				System.out.println(b_point);
				for(int j = 0; j<man.getCourse().size();j++)
				{					
					if(man.getCourse().get(i).getStu_name().equals(man.getCourse().get(j).getStu_name()) && !man.getCourse().get(i).getC_id().equals(man.getCourse().get(j).getC_id()))
					{
						c_name[n] = man.getCourse().get(j).getC_name();
						grade[n] = man.getCourse().get(j).getGrade();
						n++;
						a_point = a_point + man.getCourse().get(j).getC_point()*man.getCourse().get(j).getGpa();
						b_point = b_point + man.getCourse().get(j).getC_point();
						System.out.println(a_point);
						System.out.println(b_point);
					}
				}
			
			json.put("c_name", c_name);
			json.put("grade", grade);
			point = a_point/b_point;
			json.put("point", point);
			jsons.add(json);
			System.out.println(jsons.toString());
		}
		//System.out.println(size);
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println(jsons.toString());
		out.flush();
		out.close();
	}

}
